<template>
	<view>
		<view class="gaps">

		</view>
		<view class="add-goods-area">
			<uni-forms ref="form" :rules="rules" :modelValue="form">
				<uni-forms-item label="名称" required name="name">
					<uni-easyinput placeholder="请输入优惠劵名称" v-model="form.name" />
				</uni-forms-item>
				<uni-forms-item label="面值(分)" required name="price">
					<uni-easyinput placeholder="0" v-model="form.price" />
				</uni-forms-item>
				<uni-forms-item label="库存">
					<uni-easyinput placeholder="请输入库存" v-model="form.stock" type="number" />
				</uni-forms-item>
				<uni-forms-item label="权重">
					<uni-easyinput placeholder="请输入权重" v-model="form.weight" type="number" />
				</uni-forms-item>
				<uni-forms-item label="有效期(小时)" required>
					<uni-easyinput placeholder="请输有效期" v-model="form.expirationDate" type="number" />
				</uni-forms-item>
				<uni-forms-item label="描述">
					<uni-easyinput type="textarea" placeholder="描述信息,最多2000字" v-model="form.desc" />
				</uni-forms-item>
			</uni-forms>
		</view>
		<view class="gaps-bottom">

		</view>

		<view class="bottom-fiex-area">
			<button style="width: 100%;" type="primary" @click="addCouponForm">确认添加</button>
		</view>
	</view>
</template>

<script>
	
	import {
		addCoupon
	} from '@/api/shops/lotteryManage/lotteryManage.js'
	import {
		addPrize,
		getPrizeById,
		editPrize
	} from '@/api/shops/prizeManage/prizeManage.js'
	import uploadFile from '@/utils/uploadFile.js'
	export default {
		data() {
			return {
				eventChannel: undefined,
				timeRange: [],
				form: {
					name: '',
					price: 0,
					stock: 0,
					expirationDate:0,
					weight: 0,
					desc: '',
					type:2
				},
				rules: {
					// 对name字段进行必填验证
					name: {
						// name 字段的校验规则
						rules: [
							// 校验 name 不能为空
							{
								required: true,
								errorMessage: '请填写商品名',
							},
						],
					}
				}
			}
		},
		onLoad: function(option) {
			this.eventChannel = this.getOpenerEventChannel();
			this.form.shopId = option.shopId
			this.init()
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			init() {

			},
			addCouponForm(form) {
				let _this = this;
				this.$refs.form.validate().then(res => {
					addPrize(this.form).then((res) => {
						this.eventChannel.emit('prizeListChange', {
							data: true
						})
						uni.showModal({
							title: '添加成功',
							showCancel: false,
							success() {
								uni.navigateBack()
							}
						})
					})
				}).finally(() => {
					uni.hideLoading()
				})
			},

		}
	}
</script>

<style lang="scss">
	.add-goods-area {
		background-color: #fff;
		padding: 10px 15px;
	}

	.gaps-bottom {
		height: 60px;
	}

	.bottom-fiex-area {
		background-color: #fff;
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		height: 50px;
		display: flex;
		justify-content: center;
		align-items: center;
		z-index: 2;

	}

	.cate-sel {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		font-size: 13px;
		color: #999999;
		height: 100%;
	}

	.scroll-Y {
		height: 500rpx;
		white-space: nowrap;
		width: 100%;
		background-color: #fff;
	}
</style>